home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / arraymodule.c < prev    next >
C/C++ Source or Header  |  1998-01-25  |  28KB  |  1,336 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Array object implementation */
  33.  
  34. /* An array is a uniform list -- all items have the same type.
  35.    The item type is restricted to simple C types like int or float */
  36.  
  37. #include "Python.h"
  38.  
  39. #ifdef STDC_HEADERS
  40. #include <stddef.h>
  41. #else
  42. #include <sys/types.h>        /* For size_t */
  43. #endif
  44.  
  45. struct arrayobject; /* Forward */
  46.  
  47. struct arraydescr {
  48.     int typecode;
  49.     int itemsize;
  50.     PyObject * (*getitem) Py_FPROTO((struct arrayobject *, int));
  51.     int (*setitem) Py_FPROTO((struct arrayobject *, int, PyObject *));
  52. };
  53.  
  54. typedef struct arrayobject {
  55.     PyObject_VAR_HEAD
  56.     char *ob_item;
  57.     struct arraydescr *ob_descr;
  58. } arrayobject;
  59.  
  60. staticforward PyTypeObject Arraytype;
  61.  
  62. #define is_arrayobject(op) ((op)->ob_type == &Arraytype)
  63.  
  64. /* Forward */
  65. static PyObject *newarrayobject Py_PROTO((int, struct arraydescr *));
  66. #if 0
  67. static int getarraysize Py_PROTO((PyObject *));
  68. #endif
  69. static PyObject *getarrayitem Py_PROTO((PyObject *, int));
  70. static int setarrayitem Py_PROTO((PyObject *, int, PyObject *));
  71. #if 0
  72. static int insarrayitem Py_PROTO((PyObject *, int, PyObject *));
  73. static int addarrayitem Py_PROTO((PyObject *, PyObject *));
  74. #endif
  75. #include "protos/arraymodule_protos.h"
  76.  
  77. static PyObject *
  78. c_getitem(ap, i)
  79.     arrayobject *ap;
  80.     int i;
  81. {
  82.     return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
  83. }
  84.  
  85. static int
  86. c_setitem(ap, i, v)
  87.     arrayobject *ap;
  88.     int i;
  89.     PyObject *v;
  90. {
  91.     char x;
  92.     if (!PyArg_Parse(v, "c;array item must be char", &x))
  93.         return -1;
  94.     if (i >= 0)
  95.              ((char *)ap->ob_item)[i] = x;
  96.     return 0;
  97. }
  98.  
  99. static PyObject *
  100. b_getitem(ap, i)
  101.     arrayobject *ap;
  102.     int i;
  103. {
  104.     long x = ((char *)ap->ob_item)[i];
  105.     if (x >= 128)
  106.         x -= 256;
  107.     return PyInt_FromLong(x);
  108. }
  109.  
  110. static int
  111. b_setitem(ap, i, v)
  112.     arrayobject *ap;
  113.     int i;
  114.     PyObject *v;
  115. {
  116.     char x;
  117.     if (!PyArg_Parse(v, "b;array item must be integer", &x))
  118.         return -1;
  119.     if (i >= 0)
  120.              ((char *)ap->ob_item)[i] = x;
  121.     return 0;
  122. }
  123.  
  124. static PyObject *
  125. BB_getitem(ap, i)
  126.     arrayobject *ap;
  127.     int i;
  128. {
  129.     long x = ((unsigned char *)ap->ob_item)[i];
  130.     return PyInt_FromLong(x);
  131. }
  132.  
  133. #define BB_setitem b_setitem
  134.  
  135. static PyObject *
  136. h_getitem(ap, i)
  137.     arrayobject *ap;
  138.     int i;
  139. {
  140.     return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
  141. }
  142.  
  143. static int
  144. h_setitem(ap, i, v)
  145.     arrayobject *ap;
  146.     int i;
  147.     PyObject *v;
  148. {
  149.     short x;
  150.     if (!PyArg_Parse(v, "h;array item must be integer", &x))
  151.         return -1;
  152.     if (i >= 0)
  153.              ((short *)ap->ob_item)[i] = x;
  154.     return 0;
  155. }
  156.  
  157. static PyObject *
  158. HH_getitem(ap, i)
  159.     arrayobject *ap;
  160.     int i;
  161. {
  162.     return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
  163. }
  164.  
  165. #define HH_setitem h_setitem
  166.  
  167. static PyObject *
  168. i_getitem(ap, i)
  169.     arrayobject *ap;
  170.     int i;
  171. {
  172.     return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
  173. }
  174.  
  175. static int
  176. i_setitem(ap, i, v)
  177.     arrayobject *ap;
  178.     int i;
  179.     PyObject *v;
  180. {
  181.     int x;
  182.     if (!PyArg_Parse(v, "i;array item must be integer", &x))
  183.         return -1;
  184.     if (i >= 0)
  185.              ((int *)ap->ob_item)[i] = x;
  186.     return 0;
  187. }
  188.  
  189. static PyObject *
  190. II_getitem(ap, i)
  191.     arrayobject *ap;
  192.     int i;
  193. {
  194.     return PyLong_FromUnsignedLong(
  195.         (unsigned long) ((unsigned int *)ap->ob_item)[i]);
  196. }
  197.  
  198. static int
  199. II_setitem(ap, i, v)
  200.     arrayobject *ap;
  201.     int i;
  202.     PyObject *v;
  203. {
  204.     unsigned long x;
  205.     if (PyLong_Check(v)) {
  206.         x = PyLong_AsUnsignedLong(v);
  207.         if (x == (unsigned long) -1 && PyErr_Occurred())
  208.             return -1;
  209.     }
  210.     else {
  211.         if (!PyArg_Parse(v, "l;array item must be integer", &x))
  212.             return -1;
  213.     }
  214.     if (i >= 0)
  215.         ((unsigned int *)ap->ob_item)[i] = x;
  216.     return 0;
  217. }
  218.  
  219. static PyObject *
  220. l_getitem(ap, i)
  221.     arrayobject *ap;
  222.     int i;
  223. {
  224.     return PyInt_FromLong(((long *)ap->ob_item)[i]);
  225. }
  226.  
  227. static int
  228. l_setitem(ap, i, v)
  229.     arrayobject *ap;
  230.     int i;
  231.     PyObject *v;
  232. {
  233.     long x;
  234.     if (!PyArg_Parse(v, "l;array item must be integer", &x))
  235.         return -1;
  236.     if (i >= 0)
  237.              ((long *)ap->ob_item)[i] = x;
  238.     return 0;
  239. }
  240.  
  241. static PyObject *
  242. LL_getitem(ap, i)
  243.     arrayobject *ap;
  244.     int i;
  245. {
  246.     return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
  247. }
  248.  
  249. static int
  250. LL_setitem(ap, i, v)
  251.     arrayobject *ap;
  252.     int i;
  253.     PyObject *v;
  254. {
  255.     unsigned long x;
  256.     if (PyLong_Check(v)) {
  257.         x = PyLong_AsUnsignedLong(v);
  258.         if (x == (unsigned long) -1 && PyErr_Occurred())
  259.             return -1;
  260.     }
  261.     else {
  262.         if (!PyArg_Parse(v, "l;array item must be integer", &x))
  263.             return -1;
  264.     }
  265.     if (i >= 0)
  266.         ((unsigned long *)ap->ob_item)[i] = x;
  267.     return 0;
  268. }
  269.  
  270. static PyObject *
  271. f_getitem(ap, i)
  272.     arrayobject *ap;
  273.     int i;
  274. {
  275.     return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
  276. }
  277.  
  278. static int
  279. f_setitem(ap, i, v)
  280.     arrayobject *ap;
  281.     int i;
  282.     PyObject *v;
  283. {
  284.     float x;
  285.     if (!PyArg_Parse(v, "f;array item must be float", &x))
  286.         return -1;
  287.     if (i >= 0)
  288.              ((float *)ap->ob_item)[i] = x;
  289.     return 0;
  290. }
  291.  
  292. static PyObject *
  293. d_getitem(ap, i)
  294.     arrayobject *ap;
  295.     int i;
  296. {
  297.     return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
  298. }
  299.  
  300. static int
  301. d_setitem(ap, i, v)
  302.     arrayobject *ap;
  303.     int i;
  304.     PyObject *v;
  305. {
  306.     double x;
  307.     if (!PyArg_Parse(v, "d;array item must be float", &x))
  308.         return -1;
  309.     if (i >= 0)
  310.              ((double *)ap->ob_item)[i] = x;
  311.     return 0;
  312. }
  313.  
  314. /* Description of types */
  315. static struct arraydescr descriptors[] = {
  316.     {'c', sizeof(char), c_getitem, c_setitem},
  317.     {'b', sizeof(char), b_getitem, b_setitem},
  318.     {'B', sizeof(char), BB_getitem, BB_setitem},
  319.     {'h', sizeof(short), h_getitem, h_setitem},
  320.     {'H', sizeof(short), HH_getitem, HH_setitem},
  321.     {'i', sizeof(int), i_getitem, i_setitem},
  322.     {'I', sizeof(int), II_getitem, II_setitem},
  323.     {'l', sizeof(long), l_getitem, l_setitem},
  324.     {'L', sizeof(long), LL_getitem, LL_setitem},
  325.     {'f', sizeof(float), f_getitem, f_setitem},
  326.     {'d', sizeof(double), d_getitem, d_setitem},
  327.     {'\0', 0, 0, 0} /* Sentinel */
  328. };
  329. /* If we ever allow items larger than double, we must change reverse()! */
  330.     
  331.  
  332. static PyObject *
  333. newarrayobject(size, descr)
  334.     int size;
  335.     struct arraydescr *descr;
  336. {
  337.     arrayobject *op;
  338.     size_t nbytes;
  339.     if (size < 0) {
  340.         PyErr_BadInternalCall();
  341.         return NULL;
  342.     }
  343.     nbytes = size * descr->itemsize;
  344.     /* Check for overflow */
  345.     if (nbytes / descr->itemsize != (size_t)size) {
  346.         return PyErr_NoMemory();
  347.     }
  348.     op = PyMem_NEW(arrayobject, 1);
  349.     if (op == NULL) {
  350.         return PyErr_NoMemory();
  351.     }
  352.     if (size <= 0) {
  353.         op->ob_item = NULL;
  354.     }
  355.     else {
  356.         op->ob_item = PyMem_NEW(char, nbytes);
  357.         if (op->ob_item == NULL) {
  358.             PyMem_DEL(op);
  359.             return PyErr_NoMemory();
  360.         }
  361.     }
  362.     op->ob_type = &Arraytype;
  363.     op->ob_size = size;
  364.     op->ob_descr = descr;
  365.     _Py_NewReference(op);
  366.     return (PyObject *) op;
  367. }
  368.  
  369. #if 0
  370. static int
  371. getarraysize(op)
  372.     PyObject *op;
  373. {
  374.     if (!is_arrayobject(op)) {
  375.         PyErr_BadInternalCall();
  376.         return -1;
  377.     }
  378.     return ((arrayobject *)op) -> ob_size;
  379. }
  380. #endif
  381.  
  382. static PyObject *
  383. getarrayitem(op, i)
  384.     PyObject *op;
  385.     int i;
  386. {
  387.     register arrayobject *ap;
  388.     if (!is_arrayobject(op)) {
  389.         PyErr_BadInternalCall();
  390.         return NULL;
  391.     }
  392.     ap = (arrayobject *)op;
  393.     if (i < 0 || i >= ap->ob_size) {
  394.         PyErr_SetString(PyExc_IndexError, "array index out of range");
  395.         return NULL;
  396.     }
  397.     return (*ap->ob_descr->getitem)(ap, i);
  398. }
  399.  
  400. static int
  401. ins1(self, where, v)
  402.     arrayobject *self;
  403.     int where;
  404.     PyObject *v;
  405. {
  406.     char *items;
  407.     if (v == NULL) {
  408.         PyErr_BadInternalCall();
  409.         return -1;
  410.     }
  411.     if ((*self->ob_descr->setitem)(self, -1, v) < 0)
  412.         return -1;
  413.     items = self->ob_item;
  414.     PyMem_RESIZE(items, char,
  415.              (self->ob_size+1) * self->ob_descr->itemsize);
  416.     if (items == NULL) {
  417.         PyErr_NoMemory();
  418.         return -1;
  419.     }
  420.     if (where < 0)
  421.         where = 0;
  422.     if (where > self->ob_size)
  423.         where = self->ob_size;
  424.     memmove(items + (where+1)*self->ob_descr->itemsize,
  425.         items + where*self->ob_descr->itemsize,
  426.         (self->ob_size-where)*self->ob_descr->itemsize);
  427.     self->ob_item = items;
  428.     self->ob_size++;
  429.     return (*self->ob_descr->setitem)(self, where, v);
  430. }
  431.  
  432. #if 0
  433. static int
  434. insarrayitem(op, where, newitem)
  435.     PyObject *op;
  436.     int where;
  437.     PyObject *newitem;
  438. {
  439.     if (!is_arrayobject(op)) {
  440.         PyErr_BadInternalCall();
  441.         return -1;
  442.     }
  443.     return ins1((arrayobject *)op, where, newitem);
  444. }
  445.  
  446. static int
  447. addarrayitem(op, newitem)
  448.     PyObject *op;
  449.     PyObject *newitem;
  450. {
  451.     if (!is_arrayobject(op)) {
  452.         PyErr_BadInternalCall();
  453.         return -1;
  454.     }
  455.     return ins1((arrayobject *)op,
  456.         (int) ((arrayobject *)op)->ob_size, newitem);
  457. }
  458. #endif
  459.  
  460. /* Methods */
  461.  
  462. static void
  463. array_dealloc(op)
  464.     arrayobject *op;
  465. {
  466.     if (op->ob_item != NULL)
  467.         PyMem_DEL(op->ob_item);
  468.     PyMem_DEL(op);
  469. }
  470.  
  471. static int
  472. array_compare(v, w)
  473.     arrayobject *v, *w;
  474. {
  475.     int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
  476.     int i;
  477.     for (i = 0; i < len; i++) {
  478.         PyObject *ai, *bi;
  479.         int cmp;
  480.         ai = getarrayitem((PyObject *)v, i);
  481.         bi = getarrayitem((PyObject *)w, i);
  482.         if (ai && bi)
  483.             cmp = PyObject_Compare(ai, bi);
  484.         else
  485.             cmp = -1;
  486.         Py_XDECREF(ai);
  487.         Py_XDECREF(bi);
  488.         if (cmp != 0)
  489.             return cmp;
  490.     }
  491.     return v->ob_size - w->ob_size;
  492. }
  493.  
  494. static int
  495. array_length(a)
  496.     arrayobject *a;
  497. {
  498.     return a->ob_size;
  499. }
  500.  
  501. static PyObject *
  502. array_item(a, i)
  503.     arrayobject *a;
  504.     int i;
  505. {
  506.     if (i < 0 || i >= a->ob_size) {
  507.         PyErr_SetString(PyExc_IndexError, "array index out of range");
  508.         return NULL;
  509.     }
  510.     return getarrayitem((PyObject *)a, i);
  511. }
  512.  
  513. static PyObject *
  514. array_slice(a, ilow, ihigh)
  515.     arrayobject *a;
  516.     int ilow, ihigh;
  517. {
  518.     arrayobject *np;
  519.     if (ilow < 0)
  520.         ilow = 0;
  521.     else if (ilow > a->ob_size)
  522.         ilow = a->ob_size;
  523.     if (ihigh < 0)
  524.         ihigh = 0;
  525.     if (ihigh < ilow)
  526.         ihigh = ilow;
  527.     else if (ihigh > a->ob_size)
  528.         ihigh = a->ob_size;
  529.     np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
  530.     if (np == NULL)
  531.         return NULL;
  532.     memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
  533.            (ihigh-ilow) * a->ob_descr->itemsize);
  534.     return (PyObject *)np;
  535. }
  536.  
  537. static PyObject *
  538. array_concat(a, bb)
  539.     arrayobject *a;
  540.     PyObject *bb;
  541. {
  542.     int size;
  543.     arrayobject *np;
  544.     if (!is_arrayobject(bb)) {
  545.         PyErr_BadArgument();
  546.         return NULL;
  547.     }
  548. #define b ((arrayobject *)bb)
  549.     if (a->ob_descr != b->ob_descr) {
  550.         PyErr_BadArgument();
  551.         return NULL;
  552.     }
  553.     size = a->ob_size + b->ob_size;
  554.     np = (arrayobject *) newarrayobject(size, a->ob_descr);
  555.     if (np == NULL) {
  556.         return NULL;
  557.     }
  558.     memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
  559.     memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
  560.            b->ob_item, b->ob_size*b->ob_descr->itemsize);
  561.     return (PyObject *)np;
  562. #undef b
  563. }
  564.  
  565. static PyObject *
  566. array_repeat(a, n)
  567.     arrayobject *a;
  568.     int n;
  569. {
  570.     int i;
  571.     int size;
  572.     arrayobject *np;
  573.     char *p;
  574.     int nbytes;
  575.     if (n < 0)
  576.         n = 0;
  577.     size = a->ob_size * n;
  578.     np = (arrayobject *) newarrayobject(size, a->ob_descr);
  579.     if (np == NULL)
  580.         return NULL;
  581.     p = np->ob_item;
  582.     nbytes = a->ob_size * a->ob_descr->itemsize;
  583.     for (i = 0; i < n; i++) {
  584.         memcpy(p, a->ob_item, nbytes);
  585.         p += nbytes;
  586.     }
  587.     return (PyObject *) np;
  588. }
  589.  
  590. static int
  591. array_ass_slice(a, ilow, ihigh, v)
  592.     arrayobject *a;
  593.     int ilow, ihigh;
  594.     PyObject *v;
  595. {
  596.     char *item;
  597.     int n; /* Size of replacement array */
  598.     int d; /* Change in size */
  599. #define b ((arrayobject *)v)
  600.     if (v == NULL)
  601.         n = 0;
  602.     else if (is_arrayobject(v)) {
  603.         n = b->ob_size;
  604.         if (a == b) {
  605.             /* Special case "a[i:j] = a" -- copy b first */
  606.             int ret;
  607.             v = array_slice(b, 0, n);
  608.             ret = array_ass_slice(a, ilow, ihigh, v);
  609.             Py_DECREF(v);
  610.             return ret;
  611.         }
  612.         if (b->ob_descr != a->ob_descr) {
  613.             PyErr_BadArgument();
  614.             return -1;
  615.         }
  616.     }
  617.     else {
  618.         PyErr_BadArgument();
  619.         return -1;
  620.     }
  621.     if (ilow < 0)
  622.         ilow = 0;
  623.     else if (ilow > a->ob_size)
  624.         ilow = a->ob_size;
  625.     if (ihigh < 0)
  626.         ihigh = 0;
  627.     if (ihigh < ilow)
  628.         ihigh = ilow;
  629.     else if (ihigh > a->ob_size)
  630.         ihigh = a->ob_size;
  631.     item = a->ob_item;
  632.     d = n - (ihigh-ilow);
  633.     if (d < 0) { /* Delete -d items */
  634.         memmove(item + (ihigh+d)*a->ob_descr->itemsize,
  635.             item + ihigh*a->ob_descr->itemsize,
  636.             (a->ob_size-ihigh)*a->ob_descr->itemsize);
  637.         a->ob_size += d;
  638.         PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
  639.                         /* Can't fail */
  640.         a->ob_item = item;
  641.     }
  642.     else if (d > 0) { /* Insert d items */
  643.         PyMem_RESIZE(item, char,
  644.                  (a->ob_size + d)*a->ob_descr->itemsize);
  645.         if (item == NULL) {
  646.             PyErr_NoMemory();
  647.             return -1;
  648.         }
  649.         memmove(item + (ihigh+d)*a->ob_descr->itemsize,
  650.             item + ihigh*a->ob_descr->itemsize,
  651.             (a->ob_size-ihigh)*a->ob_descr->itemsize);
  652.         a->ob_item = item;
  653.         a->ob_size += d;
  654.     }
  655.     if (n > 0)
  656.         memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
  657.                n*b->ob_descr->itemsize);
  658.     return 0;
  659. #undef b
  660. }
  661.  
  662. static int
  663. array_ass_item(a, i, v)
  664.     arrayobject *a;
  665.     int i;
  666.     PyObject *v;
  667. {
  668.     if (i < 0 || i >= a->ob_size) {
  669.         PyErr_SetString(PyExc_IndexError,
  670.                      "array assignment index out of range");
  671.         return -1;
  672.     }
  673.     if (v == NULL)
  674.         return array_ass_slice(a, i, i+1, v);
  675.     return (*a->ob_descr->setitem)(a, i, v);
  676. }
  677.  
  678. static int
  679. setarrayitem(a, i, v)
  680.     PyObject *a;
  681.     int i;
  682.     PyObject *v;
  683. {
  684.     if (!is_arrayobject(a)) {
  685.         PyErr_BadInternalCall();
  686.         return -1;
  687.     }
  688.     return array_ass_item((arrayobject *)a, i, v);
  689. }
  690.  
  691. static PyObject *
  692. ins(self, where, v)
  693.     arrayobject *self;
  694.     int where;
  695.     PyObject *v;
  696. {
  697.     if (ins1(self, where, v) != 0)
  698.         return NULL;
  699.     Py_INCREF(Py_None);
  700.     return Py_None;
  701. }
  702.  
  703. static PyObject *
  704. array_insert(self, args)
  705.     arrayobject *self;
  706.     PyObject *args;
  707. {
  708.     int i;
  709.     PyObject *v;
  710.     if (!PyArg_Parse(args, "(iO)", &i, &v))
  711.         return NULL;
  712.     return ins(self, i, v);
  713. }
  714.  
  715. static PyObject *
  716. array_buffer_info(self, args)
  717.     arrayobject *self;
  718.     PyObject *args;
  719. {
  720.     return Py_BuildValue("ll",
  721.                  (long)(self->ob_item), (long)(self->ob_size));
  722. }
  723.  
  724. static PyObject *
  725. array_append(self, args)
  726.     arrayobject *self;
  727.     PyObject *args;
  728. {
  729.     PyObject *v;
  730.     if (!PyArg_Parse(args, "O", &v))
  731.         return NULL;
  732.     return ins(self, (int) self->ob_size, v);
  733. }
  734.  
  735. static PyObject *
  736. array_byteswap(self, args)
  737.     arrayobject *self;
  738.     PyObject *args;
  739. {
  740.     char *p;
  741.     int i;
  742.     switch (self->ob_descr->itemsize) {
  743.     case 1:
  744.         break;
  745.     case 2:
  746.         for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
  747.             char p0 = p[0];
  748.             p[0] = p[1];
  749.             p[1] = p0;
  750.         }
  751.         break;
  752.     case 4:
  753.         for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
  754.             char p0 = p[0];
  755.             char p1 = p[1];
  756.             p[0] = p[3];
  757.             p[1] = p[2];
  758.             p[2] = p1;
  759.             p[3] = p0;
  760.         }
  761.         break;
  762.     case 8:
  763.         for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
  764.             char p0 = p[0];
  765.             char p1 = p[1];
  766.             char p2 = p[2];
  767.             char p3 = p[3];
  768.             p[0] = p[7];
  769.             p[1] = p[6];
  770.             p[2] = p[5];
  771.             p[3] = p[4];
  772.             p[4] = p3;
  773.             p[5] = p2;
  774.             p[6] = p1;
  775.             p[7] = p0;
  776.         }
  777.         break;
  778.     default:
  779.         PyErr_SetString(PyExc_RuntimeError,
  780.                "don't know how to byteswap this array type");
  781.         return NULL;
  782.     }
  783.     Py_INCREF(Py_None);
  784.     return Py_None;
  785. }
  786.  
  787. static PyObject *
  788. array_reverse(self, args)
  789.     arrayobject *self;
  790.     PyObject *args;
  791. {
  792.     register int itemsize = self->ob_descr->itemsize;
  793.     register char *p, *q;
  794.     char tmp[sizeof(double)]; /* Assume that's the max item size */
  795.  
  796.     if (args != NULL) {
  797.         PyErr_BadArgument();
  798.         return NULL;
  799.     }
  800.  
  801.     if (self->ob_size > 1) {
  802.         for (p = self->ob_item,
  803.              q = self->ob_item + (self->ob_size - 1)*itemsize;
  804.              p < q;
  805.              p += itemsize, q -= itemsize) {
  806.             memmove(tmp, p, itemsize);
  807.             memmove(p, q, itemsize);
  808.             memmove(q, tmp, itemsize);
  809.         }
  810.     }
  811.     
  812.     Py_INCREF(Py_None);
  813.     return Py_None;
  814. }
  815.  
  816. /* The following routines were adapted from listobject.c but not converted.
  817.    To make them work you will have to work! */
  818.  
  819. #if 0
  820. static PyObject *
  821. array_index(self, args)
  822.     arrayobject *self;
  823.     PyObject *args;
  824. {
  825.     int i;
  826.     
  827.     if (args == NULL) {
  828.         PyErr_BadArgument();
  829.         return NULL;
  830.     }
  831.     for (i = 0; i < self->ob_size; i++) {
  832.         if (PyObject_Compare(self->ob_item[i], args) == 0)
  833.             return PyInt_FromLong((long)i);
  834.         /* XXX PyErr_Occurred */
  835.     }
  836.     PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
  837.     return NULL;
  838. }
  839. #endif
  840.  
  841. #if 0
  842. static PyObject *
  843. array_count(self, args)
  844.     arrayobject *self;
  845.     PyObject *args;
  846. {
  847.     int count = 0;
  848.     int i;
  849.     
  850.     if (args == NULL) {
  851.         PyErr_BadArgument();
  852.         return NULL;
  853.     }
  854.     for (i = 0; i < self->ob_size; i++) {
  855.         if (PyObject_Compare(self->ob_item[i], args) == 0)
  856.             count++;
  857.         /* XXX PyErr_Occurred */
  858.     }
  859.     return PyInt_FromLong((long)count);
  860. }
  861. #endif
  862.  
  863. #if 0
  864. static PyObject *
  865. array_remove(self, args)
  866.     arrayobject *self;
  867.     PyObject *args;
  868. {
  869.     int i;
  870.     
  871.     if (args == NULL) {
  872.         PyErr_BadArgument();
  873.         return NULL;
  874.     }
  875.     for (i = 0; i < self->ob_size; i++) {
  876.         if (PyObject_Compare(self->ob_item[i], args) == 0) {
  877.             if (array_ass_slice(self, i, i+1,
  878.                         (PyObject *)NULL) != 0)
  879.                 return NULL;
  880.             Py_INCREF(Py_None);
  881.             return Py_None;
  882.         }
  883.         /* XXX PyErr_Occurred */
  884.     }
  885.     PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
  886.     return NULL;
  887. }
  888. #endif
  889.  
  890. static PyObject *
  891. array_fromfile(self, args)
  892.     arrayobject *self;
  893.     PyObject *args;
  894. {
  895.     PyObject *f;
  896.     int n;
  897.     FILE *fp;
  898.     if (!PyArg_Parse(args, "(Oi)", &f, &n))
  899.         return NULL;
  900.     fp = PyFile_AsFile(f);
  901.     if (fp == NULL) {
  902.         PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
  903.         return NULL;
  904.     }
  905.     if (n > 0) {
  906.         char *item = self->ob_item;
  907.         int itemsize = self->ob_descr->itemsize;
  908.         int nread;
  909.         PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
  910.         if (item == NULL) {
  911.             PyErr_NoMemory();
  912.             return NULL;
  913.         }
  914.         self->ob_item = item;
  915.         self->ob_size += n;
  916.         nread = fread(item + (self->ob_size - n) * itemsize,
  917.                   itemsize, n, fp);
  918.         if (nread < n) {
  919.             self->ob_size -= (n - nread);
  920.             PyMem_RESIZE(item, char, self->ob_size*itemsize);
  921.             self->ob_item = item;
  922.             PyErr_SetString(PyExc_EOFError,
  923.                          "not enough items in file");
  924.             return NULL;
  925.         }
  926.     }
  927.     Py_INCREF(Py_None);
  928.     return Py_None;
  929. }
  930.  
  931. static PyObject *
  932. array_tofile(self, args)
  933.     arrayobject *self;
  934.     PyObject *args;
  935. {
  936.     PyObject *f;
  937.     FILE *fp;
  938.     if (!PyArg_Parse(args, "O", &f))
  939.         return NULL;
  940.     fp = PyFile_AsFile(f);
  941.     if (fp == NULL) {
  942.         PyErr_SetString(PyExc_TypeError, "arg must be open file");
  943.         return NULL;
  944.     }
  945.     if (self->ob_size > 0) {
  946.         if ((int)fwrite(self->ob_item, self->ob_descr->itemsize,
  947.                self->ob_size, fp) != self->ob_size) {
  948.             PyErr_SetFromErrno(PyExc_IOError);
  949.             clearerr(fp);
  950.             return NULL;
  951.         }
  952.     }
  953.     Py_INCREF(Py_None);
  954.     return Py_None;
  955. }
  956.  
  957. static PyObject *
  958. array_fromlist(self, args)
  959.     arrayobject *self;
  960.     PyObject *args;
  961. {
  962.     int n;
  963.     PyObject *list;
  964.     int itemsize = self->ob_descr->itemsize;
  965.     if (!PyArg_Parse(args, "O", &list))
  966.         return NULL;
  967.     if (!PyList_Check(list)) {
  968.         PyErr_SetString(PyExc_TypeError, "arg must be list");
  969.         return NULL;
  970.     }
  971.     n = PyList_Size(list);
  972.     if (n > 0) {
  973.         char *item = self->ob_item;
  974.         int i;
  975.         PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
  976.         if (item == NULL) {
  977.             PyErr_NoMemory();
  978.             return NULL;
  979.         }
  980.         self->ob_item = item;
  981.         self->ob_size += n;
  982.         for (i = 0; i < n; i++) {
  983.             PyObject *v = PyList_GetItem(list, i);
  984.             if ((*self->ob_descr->setitem)(self,
  985.                     self->ob_size - n + i, v) != 0) {
  986.                 self->ob_size -= n;
  987.                 PyMem_RESIZE(item, char,
  988.                               self->ob_size * itemsize);
  989.                 self->ob_item = item;
  990.                 return NULL;
  991.             }
  992.         }
  993.     }
  994.     Py_INCREF(Py_None);
  995.     return Py_None;
  996. }
  997.  
  998. static PyObject *
  999. array_tolist(self, args)
  1000.     arrayobject *self;
  1001.     PyObject *args;
  1002. {
  1003.     PyObject *list = PyList_New(self->ob_size);
  1004.     int i;
  1005.     if (list == NULL)
  1006.         return NULL;
  1007.     for (i = 0; i < self->ob_size; i++) {
  1008.         PyObject *v = getarrayitem((PyObject *)self, i);
  1009.         if (v == NULL) {
  1010.             Py_DECREF(list);
  1011.             return NULL;
  1012.         }
  1013.         PyList_SetItem(list, i, v);
  1014.     }
  1015.     return list;
  1016. }
  1017.  
  1018. static PyObject *
  1019. array_fromstring(self, args)
  1020.     arrayobject *self;
  1021.     PyObject *args;
  1022. {
  1023.     char *str;
  1024.     int n;
  1025.     int itemsize = self->ob_descr->itemsize;
  1026.     if (!PyArg_Parse(args, "s#", &str, &n))
  1027.         return NULL;
  1028.     if (n % itemsize != 0) {
  1029.         PyErr_SetString(PyExc_ValueError,
  1030.                "string length not a multiple of item size");
  1031.         return NULL;
  1032.     }
  1033.     n = n / itemsize;
  1034.     if (n > 0) {
  1035.         char *item = self->ob_item;
  1036.         PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
  1037.         if (item == NULL) {
  1038.             PyErr_NoMemory();
  1039.             return NULL;
  1040.         }
  1041.         self->ob_item = item;
  1042.         self->ob_size += n;
  1043.         memcpy(item + (self->ob_size - n) * itemsize,
  1044.                str, itemsize*n);
  1045.     }
  1046.     Py_INCREF(Py_None);
  1047.     return Py_None;
  1048. }
  1049.  
  1050. static PyObject *
  1051. array_tostring(self, args)
  1052.     arrayobject *self;
  1053.     PyObject *args;
  1054. {
  1055.     if (!PyArg_Parse(args, ""))
  1056.         return NULL;
  1057.     return PyString_FromStringAndSize(self->ob_item,
  1058.                     self->ob_size * self->ob_descr->itemsize);
  1059. }
  1060.  
  1061. static PyMethodDef array_methods[] = {
  1062.     {"append",    (PyCFunction)array_append},
  1063.     {"buffer_info", (PyCFunction)array_buffer_info},
  1064.     {"byteswap",    (PyCFunction)array_byteswap},
  1065. /*    {"count",    (method)array_count},*/
  1066.     {"fromfile",    (PyCFunction)array_fromfile},
  1067.     {"fromlist",    (PyCFunction)array_fromlist},
  1068.     {"fromstring",    (PyCFunction)array_fromstring},
  1069. /*    {"index",    (method)array_index},*/
  1070.     {"insert",    (PyCFunction)array_insert},
  1071.     {"read",    (PyCFunction)array_fromfile},
  1072. /*    {"remove",    (method)array_remove},*/
  1073.     {"reverse",    (PyCFunction)array_reverse},
  1074. /*    {"sort",    (method)array_sort},*/
  1075.     {"tofile",    (PyCFunction)array_tofile},
  1076.     {"tolist",    (PyCFunction)array_tolist},
  1077.     {"tostring",    (PyCFunction)array_tostring},
  1078.     {"write",    (PyCFunction)array_tofile},
  1079.     {NULL,        NULL}        /* sentinel */
  1080. };
  1081.  
  1082. static PyObject *
  1083. array_getattr(a, name)
  1084.     arrayobject *a;
  1085.     char *name;
  1086. {
  1087.     if (strcmp(name, "typecode") == 0) {
  1088.         char tc = a->ob_descr->typecode;
  1089.         return PyString_FromStringAndSize(&tc, 1);
  1090.     }
  1091.     if (strcmp(name, "itemsize") == 0) {
  1092.         return PyInt_FromLong((long)a->ob_descr->itemsize);
  1093.     }
  1094.     if (strcmp(name, "__members__") == 0) {
  1095.         PyObject *list = PyList_New(2);
  1096.         if (list) {
  1097.             PyList_SetItem(list, 0,
  1098.                        PyString_FromString("typecode"));
  1099.             PyList_SetItem(list, 1,
  1100.                        PyString_FromString("itemsize"));
  1101.             if (PyErr_Occurred()) {
  1102.                 Py_DECREF(list);
  1103.                 list = NULL;
  1104.             }
  1105.         }
  1106.         return list;
  1107.     }
  1108.     return Py_FindMethod(array_methods, (PyObject *)a, name);
  1109. }
  1110.  
  1111. static int
  1112. array_print(a, fp, flags)
  1113.     arrayobject *a;
  1114.     FILE *fp;
  1115.     int flags;
  1116. {
  1117.     int ok = 0;
  1118.     int i, len;
  1119.     PyObject *v;
  1120.     len = a->ob_size;
  1121.     if (len == 0) {
  1122.         fprintf(fp, "array('%c')", a->ob_descr->typecode);
  1123.         return ok;
  1124.     }
  1125.     if (a->ob_descr->typecode == 'c') {
  1126.         fprintf(fp, "array('c', ");
  1127.         v = array_tostring(a, (PyObject *)NULL);
  1128.         ok = PyObject_Print(v, fp, 0);
  1129.         Py_XDECREF(v);
  1130.         fprintf(fp, ")");
  1131.         return ok;
  1132.     }
  1133.     fprintf(fp, "array('%c', [", a->ob_descr->typecode);
  1134.     for (i = 0; i < len && ok == 0; i++) {
  1135.         if (i > 0)
  1136.             fprintf(fp, ", ");
  1137.         v = (a->ob_descr->getitem)(a, i);
  1138.         ok = PyObject_Print(v, fp, 0);
  1139.         Py_XDECREF(v);
  1140.     }
  1141.     fprintf(fp, "])");
  1142.     return ok;
  1143. }
  1144.  
  1145. static PyObject *
  1146. array_repr(a)
  1147.     arrayobject *a;
  1148. {
  1149.     char buf[256];
  1150.     PyObject *s, *t, *comma, *v;
  1151.     int i, len;
  1152.     len = a->ob_size;
  1153.     if (len == 0) {
  1154.         sprintf(buf, "array('%c')", a->ob_descr->typecode);
  1155.         return PyString_FromString(buf);
  1156.     }
  1157.     if (a->ob_descr->typecode == 'c') {
  1158.         sprintf(buf, "array('c', ");
  1159.         s = PyString_FromString(buf);
  1160.         v = array_tostring(a, (PyObject *)NULL);
  1161.         t = PyObject_Repr(v);
  1162.         Py_XDECREF(v);
  1163.         PyString_ConcatAndDel(&s, t);
  1164.         PyString_ConcatAndDel(&s, PyString_FromString(")"));
  1165.         return s;
  1166.     }
  1167.     sprintf(buf, "array('%c', [", a->ob_descr->typecode);
  1168.     s = PyString_FromString(buf);
  1169.     comma = PyString_FromString(", ");
  1170.     for (i = 0; i < len && !PyErr_Occurred(); i++) {
  1171.         if (i > 0)
  1172.             PyString_Concat(&s, comma);
  1173.         v = (a->ob_descr->getitem)(a, i);
  1174.         t = PyObject_Repr(v);
  1175.         Py_XDECREF(v);
  1176.         PyString_ConcatAndDel(&s, t);
  1177.     }
  1178.     Py_XDECREF(comma);
  1179.     PyString_ConcatAndDel(&s, PyString_FromString("])"));
  1180.     return s;
  1181. }
  1182.  
  1183. static int
  1184. array_buffer_getreadbuf(self, index, ptr)
  1185.     arrayobject *self;
  1186.     int index;
  1187.     const void **ptr;
  1188. {
  1189.     if ( index != 0 ) {
  1190.         PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment");
  1191.         return -1;
  1192.     }
  1193.     *ptr = (void *)self->ob_item;
  1194.     return self->ob_size*self->ob_descr->itemsize;
  1195. }
  1196.  
  1197. static int
  1198. array_buffer_getwritebuf(self, index, ptr)
  1199.     arrayobject *self;
  1200.     int index;
  1201.     const void **ptr;
  1202. {
  1203.     if ( index != 0 ) {
  1204.         PyErr_SetString(PyExc_SystemError, "Accessing non-existent array segment");
  1205.         return -1;
  1206.     }
  1207.     *ptr = (void *)self->ob_item;
  1208.     return self->ob_size*self->ob_descr->itemsize;
  1209. }
  1210.  
  1211. static int
  1212. array_buffer_getsegcount(self, lenp)
  1213.     arrayobject *self;
  1214.     int *lenp;
  1215. {
  1216.     if ( lenp )
  1217.         *lenp = self->ob_size*self->ob_descr->itemsize;
  1218.     return 1;
  1219. }
  1220.  
  1221. static PySequenceMethods array_as_sequence = {
  1222.     (inquiry)array_length,                /*sq_length*/
  1223.     (binaryfunc)array_concat,               /*sq_concat*/
  1224.     (intargfunc)array_repeat,        /*sq_repeat*/
  1225.     (intargfunc)array_item,                /*sq_item*/
  1226.     (intintargfunc)array_slice,        /*sq_slice*/
  1227.     (intobjargproc)array_ass_item,        /*sq_ass_item*/
  1228.     (intintobjargproc)array_ass_slice,    /*sq_ass_slice*/
  1229. };
  1230.  
  1231. static PyBufferProcs array_as_buffer = {
  1232.     (getreadbufferproc)array_buffer_getreadbuf,
  1233.     (getwritebufferproc)array_buffer_getwritebuf,
  1234.     (getsegcountproc)array_buffer_getsegcount,
  1235. };
  1236.  
  1237.  
  1238. statichere PyTypeObject Arraytype = {
  1239.     PyObject_HEAD_INIT(&PyType_Type)
  1240.     0,
  1241.     "array",
  1242.     sizeof(arrayobject),
  1243.     0,
  1244.     (destructor)array_dealloc,    /*tp_dealloc*/
  1245.     (printfunc)array_print,        /*tp_print*/
  1246.     (getattrfunc)array_getattr,    /*tp_getattr*/
  1247.     0,                /*tp_setattr*/
  1248.     (cmpfunc)array_compare,        /*tp_compare*/
  1249.     (reprfunc)array_repr,        /*tp_repr*/
  1250.     0,                /*tp_as_number*/
  1251.     &array_as_sequence,        /*tp_as_sequence*/
  1252.     0,                /*tp_as_mapping*/
  1253.     0,                 /*tp_hash*/
  1254.     0,                /*tp_call*/
  1255.     0,                /*tp_str*/
  1256.     0,                /*tp_getattro*/
  1257.     0,                /*tp_setattro*/
  1258.     &array_as_buffer,        /*tp_as_buffer*/
  1259.     0,                /*tp_xxx4*/
  1260.     0,                /*tp_doc*/
  1261. };
  1262.  
  1263.  
  1264. static PyObject *
  1265. a_array(self, args)
  1266.     PyObject *self;
  1267.     PyObject *args;
  1268. {
  1269.     char c;
  1270.     PyObject *initial = NULL;
  1271.     struct arraydescr *descr;
  1272.     if (!PyArg_Parse(args, "c", &c)) {
  1273.         PyErr_Clear();
  1274.         if (!PyArg_Parse(args, "(cO)", &c, &initial))
  1275.             return NULL;
  1276.         if (!PyList_Check(initial) && !PyString_Check(initial)) {
  1277.             PyErr_SetString(PyExc_TypeError,
  1278.                  "array initializer must be list or string");
  1279.             return NULL;
  1280.         }
  1281.     }
  1282.     for (descr = descriptors; descr->typecode != '\0'; descr++) {
  1283.         if (descr->typecode == c) {
  1284.             PyObject *a;
  1285.             int len;
  1286.             if (initial == NULL || !PyList_Check(initial))
  1287.                 len = 0;
  1288.             else
  1289.                 len = PyList_Size(initial);
  1290.             a = newarrayobject(len, descr);
  1291.             if (a == NULL)
  1292.                 return NULL;
  1293.             if (len > 0) {
  1294.                 int i;
  1295.                 for (i = 0; i < len; i++) {
  1296.                     PyObject *v =
  1297.                              PyList_GetItem(initial, i);
  1298.                     if (setarrayitem(a, i, v) != 0) {
  1299.                         Py_DECREF(a);
  1300.                         return NULL;
  1301.                     }
  1302.                 }
  1303.             }
  1304.             if (initial != NULL && PyString_Check(initial)) {
  1305.                 PyObject *v =
  1306.                   array_fromstring((arrayobject *)a, initial);
  1307.                 if (v == NULL) {
  1308.                     Py_DECREF(a);
  1309.                     return NULL;
  1310.                 }
  1311.                 Py_DECREF(v);
  1312.             }
  1313.             return a;
  1314.         }
  1315.     }
  1316.     PyErr_SetString(PyExc_ValueError,
  1317.         "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
  1318.     return NULL;
  1319. }
  1320.  
  1321. static PyMethodDef a_methods[] = {
  1322.     {"array",    a_array},
  1323.     {NULL,        NULL}        /* sentinel */
  1324. };
  1325.  
  1326. void
  1327. initarray()
  1328. {
  1329.     PyObject *m, *d;
  1330.     m = Py_InitModule("array", a_methods);
  1331.     d = PyModule_GetDict(m);
  1332.     if (PyDict_SetItemString(d, "ArrayType",
  1333.                  (PyObject *)&Arraytype) != 0)
  1334.         Py_FatalError("can't define array.ArrayType");
  1335. }
  1336.